﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestMvc3.Models;

namespace TestMvc3.Controllers
{ 
    public class StoreManagerController : Controller
    {
        private MusicStoreEntities db = new MusicStoreEntities();

        //
        // GET: /StoreManager/

        public ViewResult Index()
        {
            var albums = db.Albums;
            return View(albums);
        }

        //
        // GET: /StoreManager/Details/5

        public ViewResult Details(int id)
        {
           var result = from  item in db.Albums
                        where item.AlbumId ==id
                        select  item;
           return View(result.First<Album>());
        }

        //
        // GET: /StoreManager/Create

        public ActionResult Create()
        {
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name");
            return View();
        } 

        //
        // POST: /StoreManager/Create

        [HttpPost]
        public ActionResult Create(Album album)
        {
            if (ModelState.IsValid)
            {
                db.Albums.Add(album);
               // db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            return View(album);
        }
        
        //
        // GET: /StoreManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            var album = from item in db.Albums
                          where item.AlbumId == id
                          select item;
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.First<Album>().AlbumId);
            return View(album.First<Album>());
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Album album)
        {
            if (ModelState.IsValid)
            {
                //db.Entry(album).State = EntityState.Modified;
              //  db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.GenreId = new SelectList(db.Genres.ToArray(), "GenreId", "Name", album.GenreId);
            return View(album);
        }

        //
        // GET: /StoreManager/Delete/5
 
        public ActionResult Delete(int id)
        {
            var album = from item in db.Albums
                        where item.AlbumId == id
                        select item;
            return View(album.First<Album>());
        }

        //
        // POST: /StoreManager/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var album = from item in db.Albums
                        where item.AlbumId == id
                        select item;
            db.Albums.Remove(album.First<Album>());
           // db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
          //  db.Dispose();
            base.Dispose(disposing);
        }
    }
}